﻿// -----------------------------------------------------------------------
// <copyright file="User.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Xml.Linq;

namespace LighthouseNET
{
    using System;

    /// <summary>
    /// Class representing a public user
    /// </summary>
    public class User
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class. 
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="job">
        /// The job.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="website">
        /// The website.
        /// </param>
        private User(
            int id,
            string job,
            string name,
            string website)
        {
            Id = id;
            Job = job;
            Name = name;
            Website = website;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="job">The job.</param>
        /// <param name="website">The website.</param>
        public User(string name, string job, string website)
        {
            Job = job;
            Name = name;
            Website = website;
        }

        #region Properties
        /// <summary>
        /// Gets the id.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the job.
        /// </summary>
        public string Job { get; private set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the website.
        /// </summary>
        public string Website { get; private set; } 
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed user</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static User Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

            // ReSharper disable PossibleNullReferenceException
            var id = int.Parse(element.Element("id").Value);
            var job = element.Element("job").Value;
            var name = element.Element("name").Value;
            var website = element.Element("website").Value;

            // ReSharper restore PossibleNullReferenceException
            return new User(id, job, name, website);
        }
    }
}
